package com.java.comprehensive.arithmetic.leecode.tags.stack;

import java.util.Stack;

/**
 * @program: comprehensive-service
 *
 * @description: 有效的括号
 *
 * @author: 窦绍飞
 *
 * @create: 2021-12-16 20:41
 **/
public class LeetCode_20 {

	public boolean isValid(String s) {
		boolean flag = false;
		if (s.length() < 1) {
			return flag;
		}
		Stack<Character> stack = new Stack<>();
		for (int i = 0; i < s.length(); i++) {
			char c = s.charAt(i);
			switch (c) {
			case '(':
			case '[':
			case '{':
				stack.push(c);
				break; // 可选
			// 语句
			case ']':
				flag = !stack.empty() && stack.pop().equals('[');
				if (!flag) {
					return flag;
				}
				break;
			case '}':
				flag = !stack.empty() && stack.pop().equals('{');
				if (!flag) {
					return flag;
				}
				break;
			case ')':
				flag = !stack.empty() && stack.pop().equals('(');
				if (!flag) {
					return flag;
				}
				break;
			default: // 可选
				// 语句
				return flag;
			}
		}
		return stack.empty() && flag;
	}

	public static void main(String[] args) {
		LeetCode_20 leetCode_20 = new LeetCode_20();
		boolean valid = leetCode_20.isValid("({{{{}}}))");
		System.out.println(valid);
	}
}
